/**
 * 会议签到
 */
var HYQD = function() {
	var personimg = "http://192.168.0.216:8088/rsource/"; //图片服务器地址
	var mettingid;
	function changeSign(){
		alert(111);
		$.ajax({
			type : "GET",
			dataType : 'json',
			async : false,
			url : "../signrecord/sign/"+mettingid+"/"+click.personId,
			success : function(result) {
				console.info(result);
				if (result.success) {
					/*
					 * 修改替换图片路径
					 */
					var imgsrc=$('#'+click.personId).find('img').attr('src',personimg +$('#'+click.personId).attr("cardno")+".png");
					scanCode(result.msg);
				}else {
					scanCode(result.msg);
				}
				//setTimeout(changeSign,3000);
			}
		});
	}
	
	/**
	 * 初始化内容
	 */
	var handleContent = function() {
		var themeId = CONFIG.getUrlParam().tid;
		var typeIds = CONFIG.getUrlParam().typeids;
		$.get("../person/getbytheme/"+themeId,function(data){
			data = eval("("+data+")");
//			console.info(data);
			data.msg = eval("("+data.msg+")");
			var contentHtml='';
			if(data.success){
				$.each(data.obj,function(i){
					contentHtml+="<div class='person'><div class='photo' id='"+this.id+"' cardno = '"+this.identity+"'><img src='../assets/frontend/images/hyqd/def.png'/></div><div class='name'>"+this.name+"</div></div>";
				});
				$(".content_main_top").html(contentHtml);
				//setTimeout(changeSign,3000);
				goPage(1,12);
			} 
		});
	};

	//提示签到信息
	// var scancodeSign = {
	// 	resMsg:{
	// 		num:0,
	// 		msg:'签到'
	// 	}
	// };
	var scanCode = function (msg) {
			$('.footer').empty().text(msg).show();
			setTimeout(function () {
				$('.footer').hide();
			},2000)
	};
	
	/**
	 * 生成会议
	 */
	var handleMetting = function(){
		$.post('../metting/add/'+CONFIG.getUrlParam().tid,function (data) {
			data=eval("("+data+")");
			if(data.success){
				mettingid=data.obj;
				$.post("../mettingrecord/add/"+data.obj+"/"+CONFIG.getUrlParam().typeids,function(result){
				});
				$.get("../signrecord/connect",function(res){
					data=eval("("+res+")");
					if(res.success){
						$(".msg").html(res.msg);
					}else{
						$(".msg").html(res.msg);
					}
				});
			}else{
				$(".msg").html(data.msg);
			}
		});
	};
	
	//分页显示
	var goPage = function(pno, psize) {
				var iperson = document.getElementsByClassName('person');
				var num = iperson.length; //所有记录数
				//console.log(num);
				var totalPage = 0; //总页数
				var pageSize = psize; //每页显示行数
				//总共分几页 
				if(num / pageSize > parseInt(num / pageSize)) {
					totalPage = parseInt(num / pageSize) + 1;
				} else {
					totalPage = parseInt(num / pageSize);
				}
				click.totalPage = totalPage;
				var currentPage = pno; //当前页数
				var startRow = (currentPage - 1) * pageSize + 1; //开始显示的行  31 
				var endRow = currentPage * pageSize; //结束显示的行   40
				endRow = (endRow > num) ? num : endRow;
		        click.showNumber = (endRow - startRow+1);
				//console.log(endRow);
				//遍历显示数据实现分页
				for(var i = 1; i < (num + 1); i++) {
					var person = iperson[i - 1];
					if(i >= startRow && i <= endRow) {
						person.style.display = "block";
					} else {
						person.style.display = "none";
					}
				}

			};
	
	var btnData = [
		{"id":1,"content":"上一页"},
		{"id":2,"content":"下一页"},
	    {"id":3,"content":"取消会议"},
	    {"id":4,"content":"开始会议"}
	];
	
	/**
	 * 初始化按钮
	 */
	var handleBtn = function(){
		var btnHtml = "";
		$.each(btnData,function(i){
			btnHtml+="<div class='btn btn_"+this.id+"'>"+this.content+"</div>";
		});
		$(".content_main_bottom").html(btnHtml);
		$(".btn_"+click.num).addClass("btn_on");
	};
	
	
	var buttonSelect = function(btn_click,Psize,row,personD){
		click.whichPerson = personD+(row-1)*6+(btn_click-1)*Psize;
		$(".btn_on").removeClass("btn_on");
		if(click.showNumber>6&&$("#"+click.whichPerson)[0] == undefined){
			click.row = 2;
		}else if($("#"+click.whichPerson)[0] == undefined){
			click.row = 1;
		}
		if($("#"+click.whichPerson)[0] == undefined){
			//click.row = 1;
			click.personD = 1;
			buttonSelect(click.btn_click,click.Psize,click.row,click.personD);
		}else {
			$(".btn_person").removeClass("btn_person");
			$("#"+click.whichPerson).parent().addClass("btn_person");
			return $("#"+click.whichPerson)[0].id;
		}
//		console.log($("#"+click.whichPerson)[0].id);
	};

	var click = {
		num : 1,
		btn_click:1,//第几页
		Psize:12, //一页显示几条数据
		totalPage:0,
		showNumber:0,
		
		region:1,
		row:1,
		personD:1,
		whichPerson:0,
		personId:0
	};

	/**
	 * 绑定键盘事件
	 */
	var handleKeyDown = function() {
		$(document).keydown(
			function(e) {
				e = e || window.event; // 取得事件对象
				var keyCode = e.which || e.keyCode; // 按键值
				switch (keyCode) {
				case 37: //left
				    if (click.region==0) {
				    	if (click.personD<6) {
				    		click.personD++;
							click.personId=buttonSelect(click.btn_click,click.Psize,click.row,click.personD);
				    	}else{
				    		if(click.row ==2){
								click.personD = 1;
								click.row=1;
								click.personId=buttonSelect(click.btn_click,click.Psize,click.row,click.personD);
							}else {
								click.personD = 1;
								click.row++;
								click.personId=buttonSelect(click.btn_click,click.Psize,click.row,click.personD);
							}
				    	}
				    } else {
				    	$(".btn_on").removeClass("btn_on");
					    if(click.num>1)
						  click.num--; 
					    $(".btn_"+click.num).addClass("btn_on");
				    }
					break;
				case 39: //right
				    if (click.region==0) {
				    	if (click.personD>1) {
				    		click.personD--;
				    	    click.personId=buttonSelect(click.btn_click,click.Psize,click.row,click.personD);
				    	}else{
							if(click.row > 1){
								click.row--;
								click.personD = 6;
								click.personId=buttonSelect(click.btn_click,click.Psize,click.row,click.personD);

							}else {
								click.personD = 6;
								click.row++;
								click.personId=buttonSelect(click.btn_click,click.Psize,click.row,click.personD);
							}
				    	}
				    } else{
				        $(".btn_on").removeClass("btn_on");
					    if(click.num<btnData.length)
						    click.num++; 
					    $(".btn_"+click.num).addClass("btn_on");	
				    }
					break;
				case 38:// up
				    if (click.region==1) {
				    	click.region=0;
						click.num=1;
						click.personId=buttonSelect(click.btn_click,click.Psize,click.row,click.personD);
				    }else if (click.region ==0) {
				    	if (click.row > 1) {
				    		click.row--;
							click.personId=buttonSelect(click.btn_click,click.Psize,click.row,click.personD);
				    	} 
				    }
					break;
				case 40:// down
				    if (click.region==0) {
				    	if (click.row < 2) {
				    		if(click.showNumber >6){
								click.row++;
								click.personId=buttonSelect(click.btn_click,click.Psize,click.row,click.personD);
							}else {
								click.region = 1;
								$(".btn_person").removeClass("btn_person");
								$(".btn_"+click.num).addClass("btn_on");
							}
				    	}else {
							click.region=1;
							click.Datas=0;
							$(".btn_person").removeClass("btn_person");
							$(".btn_"+click.num).addClass("btn_on");
						}
				    }
					break;
				case 13:// ok
					if(click.region==1){
					if(click.num == 1){
							click.btn_click--;
							click.btn_click = click.btn_click < 1 ? 1:click.btn_click;
							goPage(click.btn_click,12);
							click.row = 1;
							click.personD = 1;
						} else if(click.num==2){
							click.btn_click++;
							click.btn_click = click.btn_click > click.totalPage ? click.totalPage:click.btn_click;
							goPage(click.btn_click,12);
							click.row = 1;
							click.personD = 1;
						}
						else if(click.num==3){
							window.location.href="golaucher=true";
						}else if(click.num==4){
							$.get("../camera/getbyvillage",function(data){
							data=eval("("+data+")");
//								console.log(data);
							var task = eval("("+data.msg+")");  //返回的数据没有msg
							if(data.success){
//								console.log(mettingid+"--"+task.taskid);
								$.post("../metting/startmetting",{"mettingid":mettingid,"taskid":task.taskid},function(result){
									result=eval("("+result+")");
									if(result.success){
										window.location.href=data.obj.url+"?taskid="+task.taskid;
									}
								});
							}else{
								
							}
						});
					}
					}else {
						changeSign();
					}
					break;
				case 8:
					break;
				}
			});
	};
	return {
		init : function() {
			//document.onkeydown = evm_cms_domCtrl.trackKey;
			handleMetting();
			handleBtn();
			handleContent();
			handleKeyDown();
		}
	};

}();
HYQD.init();